package com.youka.bussiness.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.youka.bussiness.common.utils.BaseUtil;
import com.youka.bussiness.dao.entity.SysAdmin;
import com.youka.bussiness.service.SysAdminService;
import com.youka.bussiness.service.req.SysAdminReqDto;
import com.youka.bussiness.service.req.SysAdminUpdateDto;
import com.youka.bussiness.service.req.SysAdminPageDto;
import com.youka.bussiness.service.res.SysAdminDto;;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.youka.bussiness.common.bean.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import com.youka.bussiness.dao.SysAdminMapper;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 管理员 Service 实现类
 *
 * @author lfc
 * @date 2024-06-30 16:47:28
 */
@Service
public class SysAdminServiceImpl extends ServiceImpl<SysAdminMapper, SysAdmin> implements SysAdminService {

    @Resource
    private SysAdminMapper sysAdminMapper;

    /**
     * 查询详情
     *
     * @param id 查询参数
     * @return sysAdminDto
     */
    @Override
    public SysAdminDto selectById(long id) {
        SysAdminDto sysAdminDto = new SysAdminDto();
        SysAdmin sysAdmin = sysAdminMapper.selectById(id);
        if (sysAdmin != null){
            BeanUtils.copyProperties(sysAdmin,sysAdminDto);
        }
        return sysAdminDto;
    }

    /**
     * 集合查询管理员
     *
     * @param dto 查询参数
     * @return Page
     */
    @Override
    public List<SysAdminDto> selectByList(SysAdminReqDto dto) {
        LambdaQueryWrapper<SysAdmin> wrapper = new LambdaQueryWrapper();
        List<SysAdmin> SysAdmins = sysAdminMapper.selectList(wrapper);
        return BaseUtil.listToList(SysAdmins, SysAdminDto.class);
    }

    /**
     * 分页查询管理员
     *
     * @param dto 查询参数
     * @return Page
     */
    @Override
    public IPage selectByPage(SysAdminPageDto dto) {
        QueryWrapper<SysAdmin> wrapper = Page.getPageWrapper(dto);
        return Page.copyProperties(this.page(Page.defaultPage(dto.getPageIndex(), dto.getPageSize()), wrapper)
                ,SysAdminDto.class);
    }

    /**
     * 新增管理员
     *
     * @param dto 管理员
     * @return 新增结果
     */
    @Override
    public void insert(SysAdminReqDto dto) {
        SysAdmin sysAdmin = new SysAdmin();
        BeanUtils.copyProperties(dto,sysAdmin);
        sysAdmin.setCreTime(LocalDateTime.now());
        sysAdminMapper.insert(sysAdmin);
    }

    /**
       * 统计
        * @param dto
       * @return
       */
    @Override
     public  SysAdminDto selectCount(SysAdminReqDto dto){
        SysAdminDto resp = new SysAdminDto();
        sysAdminMapper.selectCount(new LambdaQueryWrapper<SysAdmin>());
        return resp;
    }

    /**
     * 更新管理员
     * (根据主键)
     *
     * @param dto 管理员
     * @return 更新结果
     */
    @Override
    public void update(SysAdminUpdateDto dto) {
        SysAdmin sysAdmin =new SysAdmin();
        BeanUtils.copyProperties(dto,sysAdmin);
        sysAdmin.setUpdTime(LocalDateTime.now());
        sysAdminMapper.updateById(sysAdmin);
    }

    /**
     * 删除管理员
     * (根据主键)
     *
     * @param id id
     * @return 删除结果
     */
    @Override
    public void deleteById(Integer id) {
        sysAdminMapper.deleteById(id);
    }


}
